//
//  PackagesViewController.m
//  OrderSeat
//
//  Created by wangwei on 13-10-15.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "PackagesViewController.h"
#import "PackageDetailsViewController.h"
#import "PackagesCell.h"

@interface PackagesViewController ()
{
    int totalCount;
    float totalMoney;
}

@end

@implementation PackagesViewController


- (id)initWithShopId:(NSString*)shopId
{
    self = [super initWithNibClass:[PackagesViewController class]];
    if (self) {
        self.shopId = shopId;
        _dataSource = [[PackagesDataSource alloc] initWithVC:self];
        [_dataSource fetchPackages];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI
{
    [self setNavigationTitle:@"套餐"];
    [self createLeftButton];
    
    self.packagesTable.delegate = _dataSource;
    self.packagesTable.dataSource = _dataSource;
}

- (void)selectDishCountFinished
{
    
}

@end

//=======================================================================================
#pragma============数据源实现==============================================================
//=======================================================================================

@implementation PackagesDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if (self) {
        self.viewController = viewController;
        self.packageList = [[NSMutableArray alloc] initWithCapacity:10];
    }
    
    return self;
}

- (void)fetchPackages
{
    NSString *url = [NSString stringWithFormat:API_PACKAGE_LIST,self.viewController.shopId];
    [self get:url];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"request:%@", request.responseString);
    
    NSDictionary *json = [request.responseData JSONValue];
    
    if ([[json objectForKey:@"error_code"] intValue] != 0) {
        return;
    }
    
    self.packageList = [json objectForKey:@"taocan"];
    
    for (NSMutableDictionary *dic in self.packageList) {
        [dic setObject:@"0" forKey:@"fenshu"];
    }
    
    [self.viewController.packagesTable reloadData];
}

#pragma ===================加载tableview数据 Begin================================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.packageList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PackagesCell";
    
    PackagesCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"PackagesCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    NSMutableDictionary *dic = [self.packageList objectAtIndex:indexPath.row];
    [cell setContent:dic];
    cell.delegate = self.viewController;
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dic = [self.packageList objectAtIndex:indexPath.row];
    [dic setObject:@"1" forKey:@"fenshu"];
    
    NSString *shopId = self.viewController.shopId;
    NSString *packageId = [dic objectForKey:@"id"];
    
    PackageDetailsViewController *detailVC = [[PackageDetailsViewController alloc] initWithShopId:shopId packageId:packageId];
    detailVC.packageEntity = dic;
    detailVC.isNext = _viewController.isNext;
    detailVC.selectSeats = _viewController.selectSeats;
    [self.viewController.navigationController pushViewController:detailVC animated:YES];
}

#pragma ===================加载tableview数据 End===============================================

@end
